package com.youkeyi.ddy.cloud.basic.hr.employee.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youkeyi.ddy.cloud.basic.hr.employee.entity.TblEmployeeCompensationBenefits;
import com.youkeyi.ddy.cloud.basic.hr.employee.service.TblEmployeeCompensationBenefitsService;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.compensation.HrEmployeeCompensationBenefitsCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.compensation.dto.HrEmployeeCompensationBenefitsListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.compensation.dto.HrEmployeeCompensationBenefitsListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.compensation.param.HrEmployeeCompensationBenefitsCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.compensation.param.HrEmployeeCompensationBenefitsDeleteCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.compensation.param.HrEmployeeCompensationBenefitsQueryCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.compensation.param.HrEmployeeCompensationBenefitsUpdateCloudParam;
import com.youkeyi.ddy.cloud.common.constant.YoukeyiDdyCloudBasicPublicConstant;
import com.youkeyi.ddy.cloud.common.enums.ResponseCodeEnum;
import com.youkeyi.ddy.cloud.common.response.ResponseContent;
import com.youkeyi.ddy.cloud.common.utils.AESUtils;
import com.youkeyi.ddy.cloud.common.utils.SimpleUUidUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 员工薪酬福利表 前端控制器
 * </p>
 *
 * @author xiawang.liu
 * @since 2024-04-28
 */
@Service
public class TblEmployeeCompensationBenefitsController implements HrEmployeeCompensationBenefitsCloudService {

    @Autowired
    private TblEmployeeCompensationBenefitsService tblEmployeeCompensationBenefitsService;

    @Override
    public HrEmployeeCompensationBenefitsListCloudDto query(HrEmployeeCompensationBenefitsQueryCloudParam param) {
        HrEmployeeCompensationBenefitsListCloudDto dto = new HrEmployeeCompensationBenefitsListCloudDto();
        if (null == param) {
            dto.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return dto;
        }

        if (CollectionUtils.isNotEmpty(param.getEmployeeCompensationBenefitsPrimaryKeys())) {
            param.getEmployeeCompensationBenefitsPrimaryKeys().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getEmployeePrimaryKeys())) {
            param.getEmployeePrimaryKeys().removeAll(Collections.singleton(null));
        }

        QueryWrapper<TblEmployeeCompensationBenefits> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .in(CollectionUtils.isNotEmpty(param.getEmployeeCompensationBenefitsPrimaryKeys()), TblEmployeeCompensationBenefits::getPrimaryKey, param.getEmployeeCompensationBenefitsPrimaryKeys())
                .in(CollectionUtils.isNotEmpty(param.getEmployeePrimaryKeys()), TblEmployeeCompensationBenefits::getEmployeeKey, param.getEmployeePrimaryKeys())
                .ne(TblEmployeeCompensationBenefits::getIsDeleted, YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO_YES.value())
        ;

        Page<TblEmployeeCompensationBenefits> page = new Page<>();
        if (null != param.getCurrentPage() && null != param.getPageSize()) {
            page.setCurrent(param.getCurrentPage());
            page.setSize(param.getPageSize());
        } else {
            page.setCurrent(1);
            page.setSize(99999999);
        }
        Page<TblEmployeeCompensationBenefits> employeeList = tblEmployeeCompensationBenefitsService.page(page, wrapper);
        if (CollectionUtils.isNotEmpty(employeeList.getRecords())) {
            dto.setTotal(employeeList.getTotal());

            List<HrEmployeeCompensationBenefitsListItemCloudDto> items = new ArrayList<>();
            employeeList.getRecords().forEach(employee -> {
                HrEmployeeCompensationBenefitsListItemCloudDto item = new HrEmployeeCompensationBenefitsListItemCloudDto();
                item.setEmployeeCompensationBenefitsPrimaryKey(employee.getPrimaryKey());
                item.setEmployeeKey(employee.getEmployeeKey());
                item.setSalaryAccountName(employee.getSalaryAccountName());
                item.setSalaryBank(employee.getSalaryBank());
                if (StringUtils.isNotBlank(employee.getSalaryAccountNumber())) {
                    String accountNumber;
                    try {
                        accountNumber = AESUtils.decrypt(employee.getSalaryAccountNumber());
                    } catch (Exception e) {
                        e.printStackTrace();
                        accountNumber = employee.getSalaryAccountNumber();
                    }
                    item.setSalaryAccountNumber(accountNumber);
                }
                item.setProvidentFundAccount(employee.getProvidentFundAccount());
                item.setSocialSecurityNumber(employee.getSocialSecurityNumber());
                item.setIsDeleted(employee.getIsDeleted());
                item.setIsDeletedName(YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO.getName(item.getIsDeleted()));
                item.setCreateTime(employee.getCreateTime() != null ? employee.getCreateTime().getTime() : null);
                item.setCreateUserKey(employee.getCreateUserKey());
                item.setCreateUserName(employee.getCreateUserName());
                item.setUpdateTime(employee.getUpdateTime() != null ? employee.getUpdateTime().getTime() : null);
                item.setUpdateUserKey(employee.getUpdateUserKey());
                item.setUpdateUserName(employee.getUpdateUserName());
                items.add(item);
            });
            dto.setRows(items);
        }
        dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return dto;
    }

    @Override
    public ResponseContent create(HrEmployeeCompensationBenefitsCreateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        if (null == param || StringUtils.isBlank(param.getEmployeeKey())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }
        TblEmployeeCompensationBenefits employee = new TblEmployeeCompensationBenefits();
        employee.setPrimaryKey(SimpleUUidUtils.getUuid());
        employee.setEmployeeKey(param.getEmployeeKey());
        employee.setSalaryAccountName(param.getSalaryAccountName());
        employee.setSalaryBank(param.getSalaryBank());
        employee.setSalaryAccountNumber(AESUtils.encrypt(param.getSalaryAccountNumber()));
        employee.setProvidentFundAccount(param.getProvidentFundAccount());
        employee.setSocialSecurityNumber(param.getSocialSecurityNumber());
        employee.setIsDeleted(YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO_NO.value());
        employee.setCreateUserKey(param.getCreateUserKey());
        employee.setCreateUserName(param.getCreateUserName());
        employee.setCreateTime(new Date());
        boolean save = tblEmployeeCompensationBenefitsService.save(employee);
        if (!save) {
            rc.setErrorMessage(ResponseCodeEnum.FAIL.getMessage());
            return rc;
        }
        rc.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        rc.setKey(employee.getPrimaryKey());
        return rc;
    }

    @Override
    public ResponseContent update(HrEmployeeCompensationBenefitsUpdateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        if (null == param || StringUtils.isBlank(param.getEmployeeCompensationBenefitsPrimaryKey())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        UpdateWrapper<TblEmployeeCompensationBenefits> wrapper = new UpdateWrapper<>();
        wrapper.lambda()
                .eq(TblEmployeeCompensationBenefits::getPrimaryKey, param.getEmployeeCompensationBenefitsPrimaryKey());
        TblEmployeeCompensationBenefits employee = new TblEmployeeCompensationBenefits();
        employee.setPrimaryKey(param.getEmployeeCompensationBenefitsPrimaryKey());
        employee.setSalaryAccountName(param.getSalaryAccountName());
        employee.setSalaryBank(param.getSalaryBank());
        employee.setSalaryAccountNumber(AESUtils.encrypt(param.getSalaryAccountNumber()));
        employee.setProvidentFundAccount(param.getProvidentFundAccount());
        employee.setSocialSecurityNumber(param.getSocialSecurityNumber());
        employee.setUpdateUserKey(param.getUpdateUserKey());
        employee.setUpdateUserName(param.getUpdateUserName());
        employee.setUpdateTime(new Date());
        boolean save = tblEmployeeCompensationBenefitsService.update(employee, wrapper);
        rc.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        if (!save) {
            rc.setErrorMessage(ResponseCodeEnum.FAIL.getMessage());
            return rc;
        }
        return rc;
    }

    @Override
    public ResponseContent delete(HrEmployeeCompensationBenefitsDeleteCloudParam param) {
        ResponseContent rc = new ResponseContent();
        if (null == param || CollectionUtils.isEmpty(param.getEmployeeCompensationBenefitsPrimaryKeys())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }
        UpdateWrapper<TblEmployeeCompensationBenefits> wrapper = new UpdateWrapper<>();
        wrapper.lambda()
                .in(TblEmployeeCompensationBenefits::getPrimaryKey, param.getEmployeeCompensationBenefitsPrimaryKeys());

        TblEmployeeCompensationBenefits employee = new TblEmployeeCompensationBenefits();
        employee.setIsDeleted(YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO_YES.value());
        employee.setUpdateUserKey(param.getUpdateUserKey());
        employee.setUpdateUserName(param.getUpdateUserName());
        employee.setUpdateTime(new Date());
        boolean save = tblEmployeeCompensationBenefitsService.update(employee, wrapper);
        rc.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        if (!save) {
            rc.setErrorMessage(ResponseCodeEnum.FAIL.getMessage());
        }
        return rc;
    }
}
